package com.ad.pet.app.shopcar;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ad.pet.MainActivityCompent;
import com.ad.pet.R;
import com.ad.pet.app.Navigation;
import com.ad.pet.app.shopcar.adapter.ArticleAdapter;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.fragment.BaseFragment;
import com.ad.pet.models.remote.collect.BigArticle;
import com.ad.pet.ui.SimpleDividerDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by liuyixiang on 2019/10/27.
 */

public class FindFragment extends BaseFragment implements YouLikeContract.View{


    @Inject
    YouLikePresenter youLikePresenter;
    @BindView(R.id.recycler_article)
    RecyclerView recyclerArticle;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    ArticleAdapter articleAdapter;
    Unbinder unbinder;

    int page = 1;
    @Override
    protected void initData() {
            youLikePresenter.onCreate(this);
         LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        articleAdapter = new ArticleAdapter(null);
        recyclerArticle.setLayoutManager(linearLayoutManager);
        recyclerArticle.addItemDecoration(new SimpleDividerDecoration(getActivity(),3,R.color.bg_color));
        recyclerArticle.setAdapter(articleAdapter);

        youLikePresenter.getArticleList("1",String .valueOf(page));
    }

    @Override
    protected void initEvents() {
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                youLikePresenter.getArticleList("1",String.valueOf(page));
            }
        });

        articleAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                ++page;
                youLikePresenter.getArticleList("1",String.valueOf(page));
            }
        }, recyclerArticle);

        articleAdapter.setOnzanClickLinstenr(new ArticleAdapter.OnzanClickLinstenr() {
            @Override
            public void onClick(int position) {
                if (!hasLogin()){
                    Navigation.goLogin(getActivity(),1,1);
                    return;
                }
                if (!articleAdapter.getData().get(position).isUp()){
                    youLikePresenter.zanArticle(articleAdapter.getData().get(position).getId());
                }else {
                    youLikePresenter.cancelZanArticle(articleAdapter.getData().get(position).getId());
                }

            }

            @Override
            public void onComment(int position) {

            }
        });
        articleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Navigation.goCommonWebView(getActivity(),articleAdapter.getData().get(position).getTitle(),articleAdapter.getData().get(position).getH5_url(),false,100);
            }
        });
    }

    @Override
    protected void initViews() {

    }

    @Override
    public void initInjector() {
        getComponent(MainActivityCompent.class).inject(this);
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_find;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public static FindFragment newInstance() {
        FindFragment fragment = new FindFragment();
        return fragment;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {

    }

    @Override
    public void error(int code, String msg) {
        ToastUtil.getToastUtil().showShort(msg);
    }

    @Override
    public void getArticleListSuccess(BigArticle bigArticle) {
        swipeRefresh.setRefreshing(false);
        if (page == 1) {
            articleAdapter.setNewData(bigArticle.getData1());
        } else {
            articleAdapter.addData(bigArticle.getData1());
            articleAdapter.loadMoreComplete();
        }
        if (bigArticle.getData1()!=null&&bigArticle.getData1().size() < 10) {//CommonConstant.PAGE_COUNT
            //暂无更多数据
            articleAdapter.loadMoreEnd();
        }
        if (bigArticle.getData1()==null||bigArticle.getData1().size() == 0 && page == 1) {
            articleAdapter.setEmptyView(R.layout.layout_empty);
        }
    }

    @Override
    public void getCollectArticleListSuccess(BigArticle page) {

    }

    @Override
    public void collectArticleSuccess() {

    }

    @Override
    public void cancelCollectArticleSuccess() {

    }

    @Override
    public void zanArticletSuccess() {
        youLikePresenter.getArticleList("1",String.valueOf(page));
    }

    @Override
    public void cancelZanArticleSuccess() {
        youLikePresenter.getArticleList("1",String.valueOf(page));
    }

    @Override
    public void articleCommentSuccess() {

    }
}
